<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form
          ref="form"
          :model="queryForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-select
              v-model="queryForm.enable"
              clearable
              placeholder="有效状态"
              style="width: 120px"
              @clear="handleQuery"
            >
              <el-option label="有效" :value="1" />
              <el-option label="无效" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryForm.search" />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      border
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="序号" width="55">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="key" label="KEY" width="150" />
      <el-table-column prop="name" label="名称" width="200" />
      <el-table-column prop="link_image" label="海报" width="101">
        <template #default="{ row }">
          <el-image
            v-if="row.link_image"
            style="width: 80px; height: 60px; background: rgba(0, 0, 0, 0.3)"
            :src="baseUrl + row.link_image"
            :preview-src-list="[baseUrl + row.link_image]"
            fit="contain"
          />
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="有效状态" width="80">
        <template #default="{ row }">
          <el-tag :type="row.enable | statusFilter">
            {{ row.enable ? '有效' : '无效' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="desc" label="描述" />
      <el-table-column prop="created_time" width="160px" label="创建时间" />
      <el-table-column prop="modified_time" width="160px" label="修改时间" />
      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <template #default="{ row }">
          <el-button type="text" @click="handleContentEdit(row)">
            内容编辑
          </el-button>
          <el-button type="text" @click="handleView(row)">预览</el-button>
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.limit"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <topic-edit ref="edit" @fetch-data="fetchData"></topic-edit>
  </div>
</template>

<script>
  import { getList, doDelete, doDeletes } from '@/api/topic'
  import TopicEdit from './components/TopicEdit'
  export default {
    name: 'Topic',
    components: {
      TopicEdit,
    },
    filters: {
      statusFilter(enable) {
        return enable ? 'success' : 'danger'
      },
    },
    data() {
      return {
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          page: 1,
          limit: 20,
          search: '',
        },
        baseApi: '/_topic/',
        baseUrl: '',
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      handleView(row) {
        window.open(`${this.baseUrl}${row.url}`)
      },
      handleContentEdit(row) {
        this.$router.push({
          name: 'Editor',
          params: { topicKey: row.key },
        })
      },
      tableSortChange() {},
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.key) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await doDelete(this.baseApi, row.key)
            this.$baseMessage('删除成功', 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.key)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              await doDeletes(this.baseApi, ids)
              this.$baseMessage('选中项删除成功', 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.limit = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, count } = await getList(this.baseApi, this.queryForm)
        this.list = data
        const imageList = []
        data.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
        this.total = count
        this.listLoading = false
      },
    },
  }
</script>
